package com.theo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.theo.entity.CityInfo;
import com.theo.entity.EpidemicInfo;
import com.theo.entity.NewsInfo;
import com.theo.service.CityInfoService;
import com.theo.service.NewsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 方涛
 * @since 2021-02-02
 */
@Controller
@RequestMapping("/news-info")
public class NewsInfoController {

    @Autowired
    private NewsInfoService newsInfoService;
    @Autowired
    private CityInfoService cityInfoService;

    @RequestMapping("/show")
    public String show(Model model) {
        List<NewsInfo> newsInfos = newsInfoService.queryAllNewsInfo();
        newsInfos.forEach(System.out::println);
        model.addAttribute("news", newsInfos);
        return "user/news";
    }

    @RequestMapping("/toInput.do")
    public String toInput(Model model) {
        List<CityInfo> provinces = cityInfoService.findProvinces();
        model.addAttribute("provinces", provinces);
        return "admin/news_input";
    }
    @RequestMapping("/input.do")
    public String saveNews(NewsInfo newsInfo) {
        newsInfoService.saveNewsInfo(newsInfo);
        return "redirect:/news-info/show.do";
    }

    @RequestMapping("/show.do")
    public String show(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<NewsInfo> list = newsInfoService.queryAllNewsInfo();
        //list.forEach(System.out::println);
        PageInfo pageInfo = new PageInfo(list, 6);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/news_list";
    }

    @RequestMapping("/toUpdate.do")
    public String toUpdate(Model model, Integer id) {
        NewsInfo newsInfo = newsInfoService.queryNewsInfoById(id);
        model.addAttribute("news", newsInfo);
        return "admin/news_update";
    }

    @RequestMapping("/update.do")
    public String update(NewsInfo newsInfo) {
        newsInfoService.updateNewsInfo(newsInfo);
        return "redirect:/news-info/show.do";
    }

    @RequestMapping("/delete.do")
    public String deleteNewsInfo(@RequestParam("id") Integer id) {
        newsInfoService.deleteNewsInfo(id);
        return "redirect:/news-info/show.do";
    }

    @RequestMapping("/findAllByDate.do")
    public String findAllByDate(Model model, String date, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<NewsInfo> list = newsInfoService.findAllByDate(date);
        PageInfo pageInfo = new PageInfo(list, 6);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/news_list";
    }
}

